package com.example.arrange;

import com.example.util.CommonUtils;

import java.util.Arrays;
import java.util.concurrent.CompletableFuture;

public class ThenComposeDemo {

    public static void main(String[] args) {
        CommonUtils.printThreadLog("main start");

        String path = "filter_words.txt";

        CompletableFuture<String> future = readFileFuture(path);
        CompletableFuture<String[]> split = splitFuture(future.join());
        System.out.println(Arrays.toString(split.join()));


        CompletableFuture<CompletableFuture<String[]>> future1 =
                readFileFuture(path).thenApply(ThenComposeDemo::splitFuture);
        CompletableFuture<String[]> join1 = future1.join();
        String[] join2 = join1.join();
        System.out.println(Arrays.toString(join2));

        CommonUtils.printThreadLog("main end");
    }

    private static CompletableFuture<String> readFileFuture(String path) {
        return CompletableFuture.supplyAsync(() -> {
            CommonUtils.printThreadLog("读取文件：" + path);
            return CommonUtils.readFile(path);
        });
    }

    private static CompletableFuture<String[]> splitFuture(String content) {
        return CompletableFuture.supplyAsync(() -> {
            CommonUtils.printThreadLog("将内容转为字符串数组");
            return content.split(",");
        });
    }
}
